package com.example.demospringboot.netty.http.handler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;

/**
 * @author chenlei
 * @date 2020-01-07
 * @description 客户端初始化channel
**/
public class HttpClientInitializer extends ChannelInitializer<SocketChannel>{

	private HttpResponseHandler httpResponseHandler;

	public HttpResponseHandler getHttpResponseHandler() {
		return httpResponseHandler;
	}

	public void setHttpResponseHandler(HttpResponseHandler httpResponseHandler) {
		this.httpResponseHandler = httpResponseHandler;
	}

	/*
	 * @author chenlei
	 * @date 2020-01-07
	 * @description 
	 * @see io.netty.channel.ChannelInitializer#initChannel(io.netty.channel.Channel)
	 */
	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
		pipeline.addLast(new HttpClientCodec());//httpRequestEncoder 和 httpResponseDecoder的组合
		//消息接收器
		pipeline.addLast("httpAggregator",new HttpObjectAggregator(512*1024));
		pipeline.addLast(httpResponseHandler);
	}

}
